<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		$this->load->model('Msetting');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
		load_lang("admin");
	check_login();
	}

	function index($page='')
	{
	
			$id= $this->uri->segment(4);
	if ($this->input->post('update'))

    {
	

      //set rules


    
    

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ( $this->sub_update())

        redirect('admin/setting/', 'refresh');

      else
		{
			
        	//$this->form_validation->run() &&$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/setting/', 'refresh');

		}

	

    $data['result'] = $this->Msetting->list_data($page,5);// $this->Muser->user_review($page, $limit);

    $data['title'] = 'Setting Congfigs';

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'setting_review';

    $this->load->view('admin/layout/template_main', $data);
	}

	function sub_update()
	{
		
	$id=1;
	if(isset($_POST["update"]))
		{
		
	
		$data = array("languages" => $this->input->post('language'));
		//print_r($data);
		//	echo $id;
		if($this->Msetting->update($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}
	
	
	
	function create()
	{
	 if ($this->input->post('create'))

    {
	
		
      //set rules
  $this->form_validation->set_rules('name_vi', 'Tên Nhóm(Tiếng Anh)', 'required');

      $this->form_validation->set_rules('name_en', 'Tên Nhóm(Tiếng Việt)', 'required');
    

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/setting/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/setting/', 'refresh');

		}



		 //reload template

		$data['main'] = 'sub_cate_create';

    $data['title'] = 'Thêm Nhóm Hàng';
	
	$data['cate']= $this->Msetting->get_cate();
 
		$this->load->view('admin/template_main', $data);
	}
	function check_email()
	{
		$email=$this->uri->segment(4);
		if(!$this->Muser->check_email($email))
			echo "1";
		else
			echo "0";

	}
	
	function sub_create()
	{
		if(isset($_POST["create"]))
		{
/*		if($this->Muser->check_email(!$this->input->post("email")))
		{	
			$this->form_validation->_error_array["email"] = "This Email have been existed ."; 
		
			return false;
		
		} */
		$name = array(
			"vi" => htmlentities($_POST["name_vi"],ENT_QUOTES,"UTF-8"),
			"en" => htmlentities($_POST["name_en"],ENT_QUOTES,"UTF-8"),
			);
		$data = array ( "sub_cate_name" => serialize($name),
						"cate_id" => $_POST['cate']  , 
		
		
		);
		//print_r($data);
	
			
		if($this->Msetting->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Msetting->delete();

				} break;

			}//end switch



			redirect('admin/setting/', 'refresh');

		}//end if

	}
	
}









?>
